/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package gui.components.basic.gui;

import gui.components.basic.gui.logic.Attribute;
import gui.components.basic.gui.logic.CRUDParametrizablePanel;
import java.awt.Color;
import java.awt.Font;
import javax.swing.border.Border;

/**
 *
 * @author Luca
 */
public class SimpleTextParamPanel extends javax.swing.JPanel implements CRUDParametrizablePanel {

    public Attribute attribute;
    private Border ahahBorder = null;

    /**
     * Creates new form ParamPanel
     */
    public SimpleTextParamPanel() {
        initComponents();
        this.ahahBorder = this.jTextField_Value.getBorder();
    }

    public SimpleTextParamPanel(Attribute attribute) {
        initComponents();
        this.ahahBorder = this.jTextField_Value.getBorder();
        this.attribute = attribute;
        this.jLabel1.setText(attribute.getKey());
        if (attribute.getValue() != null) {
            this.jTextField_Value.setText(attribute.getValue());
        }
    }

    public void setAttribute(Attribute attribute) {
        this.attribute = attribute;
        this.jLabel1.setText(attribute.getKey());
        if (attribute.getValue() != null) {
            this.jTextField_Value.setText(attribute.getValue());
        }
    }

    public Attribute getAttribute() {
        attribute.setKey(this.jLabel1.getText());
        attribute.setValue(this.jTextField_Value.getText());
        return attribute;
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jTextField_Value = new javax.swing.JTextField();

        jLabel1.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
        jLabel1.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        jLabel1.setText("ParamKey:");

        jTextField_Value.setMinimumSize(new java.awt.Dimension(20, 20));

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 149, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jTextField_Value, javax.swing.GroupLayout.DEFAULT_SIZE, 123, Short.MAX_VALUE)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                .addComponent(jLabel1, javax.swing.GroupLayout.DEFAULT_SIZE, 26, Short.MAX_VALUE)
                .addComponent(jTextField_Value, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
        );
    }// </editor-fold>//GEN-END:initComponents


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel jLabel1;
    private javax.swing.JTextField jTextField_Value;
    // End of variables declaration//GEN-END:variables

    @Override
    public void setEditable(boolean editable) {
        this.jTextField_Value.setEditable(editable);
        if(editable){
           this.jTextField_Value.setBorder(ahahBorder);
            this.jTextField_Value.setFont(new Font("Tahoma", Font.BOLD, 12));
            this.jTextField_Value.setForeground(Color.BLACK);
            this.jTextField_Value.setBackground(Color.WHITE);
        }else{
            this.jTextField_Value.setBorder(null);
            this.jTextField_Value.setFont(new Font("Tahoma", Font.BOLD, 12));
            this.jTextField_Value.setForeground(Color.BLUE);
            this.jTextField_Value.setBackground(Color.lightGray);
        }
    }

    @Override
    public boolean isEditable() {
        return this.jTextField_Value.isEnabled();
    }

    @Override
    public Object getValue() {
        return this.getAttribute().getValue();
    }

    @Override
    public void setValue(Object value) {
        if (value != null) {
            this.jTextField_Value.setText(value.toString());
        }
    }

    @Override
    public void clear() {
//        this.attribute = null;
        this.jTextField_Value.setText("");
    }
}
